/**
 * 
 */
package edu.gatech.oad.mule.input;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import edu.gatech.oad.mule.input.InputEvent.ButtonPress;
import edu.gatech.oad.mule.input.interfaces.ControllerOwner;

/**
 * Translates keyboard input into Controller button events.
 * 
 * @author Dale Avery
 * @version %I%, %G%
 */
public final class Keyboard extends Controller implements KeyListener {

	/**
	 * Creates a controller that accepts keyboard button presses.
	 * 
	 * @param owner
	 *            The owner of this controller.
	 */
	public Keyboard(ControllerOwner owner) {
		super(owner);

	}

	@Override
	public void keyPressed(KeyEvent e) {
		// Retrieve the code of the key that was pressed.
		int code = e.getKeyCode();
		InputEvent event = null;

		// Fire events based on which keycode was received.
		if (code == KeyEvent.VK_UP)
			event = new InputEvent(ButtonPress.UP);
		else if (code == KeyEvent.VK_DOWN)
			event = new InputEvent(ButtonPress.DOWN);
		else if (code == KeyEvent.VK_RIGHT)
			event = new InputEvent(ButtonPress.RIGHT);
		else if (code == KeyEvent.VK_LEFT)
			event = new InputEvent(ButtonPress.LEFT);

		if(event != null)
			super.buttonEvent(event);
	}

	@Override
	public void keyReleased(KeyEvent e) {
		InputEvent event = null;
		
		if (e.getKeyCode() == KeyEvent.VK_SPACE)
			event = new InputEvent(ButtonPress.ACTION);
		
		if(event != null)
			super.buttonEvent(event);
	}

	@Override
	public void keyTyped(KeyEvent e) {

	}
}
